-- UIBSPolice
-- Created by cheny3 Oct/09/2016
-- 蓝鲨警官

UIBSPolice = class("UIBSPolice", function()
    return cc.Layer:create();
end);

function UIBSPolice.create(gridData)
    return UIBSPolice.new(gridData);
end

function UIBSPolice:ctor(gridData)
    -- 初始化
    self:setName("UIBSPolice");
    -- 复用buildingView2的模板
    local node = cc.CSLoader:createNode("layout/dungeon/BuildingView2.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    assert(type(gridData) == "table", "必须传入格子数据");

    self.gridData  = gridData;
    self.index = 1;

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UIBSPolice:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册事件处理回调函数
function UIBSPolice:registerEventCallback()
    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UIBSPolice 界面析构清理");
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():showMenu();
            end

        elseif eventType == "enter" then
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():hideMenu();
            end
        end
    end);
end

-- 注册点击事件
function UIBSPolice:registerTouchEvent()
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    -- 注册返回按钮点击事件
    local node = self.node;
    local btnBack = findChildByName(node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIBSPolice");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册确定按钮点击事件
    local btnOk = findChildByName(node, "CT/btn_ok");
    local function onOkClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_select");

            local msg = buildingInfo["bonus_tip"];
            local list = splitString(msg, "\n");
            splitAlert(list);

            -- 蓝鲨警官
            DungeonActionM.go("bs_police", self.gridData:getPos());

            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIBSPolice");
        end
    end
    AddTouchEventListener(btnOk, onOkClick);

    -- 注册取消按钮点击事件
    local btnCancel = findChildByName(node, "CT/btn_cancel");
    local function onCancelClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_select");

            local msg = buildingInfo["fail_tip"];
            local list = splitString(msg, "\n");
            splitAlert(list);

            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIBSPolice");
        end
    end
    AddTouchEventListener(btnCancel, onCancelClick);

    -- 注册继续按钮点击事件
    local btnContinue = findChildByName(node, "CT/btn_one");
    local function onContinueClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_select");

            if PetM.isActivePet(121) then
                -- 准备战斗
                DungeonActionM.go("bs_police", self.gridData:getPos());

                -- 关闭当前界面
                UIMgr.getCurrentScene():removeFormByName("UIBSPolice");
            else
                self.index = self.index + 1;
                -- 重绘按钮
                self:redrawBtn();
                -- 内容
                self:redrawDialog();
            end
        end
    end
     AddTouchEventListener(btnContinue, onContinueClick);
end

-- 重绘
function UIBSPolice:redraw()
    local descLabel  = findChildByName(self.node, "CT/desc");
    local iconImg    = findChildByName(self.node, "CT/icon");
    local titleLabel = findChildByName(self.node, "CT/title_node/title");

    -- 建筑配置
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    local title = buildingInfo["title"];
    local classId = self.gridData.bishop_id;

    -- 标题
    titleLabel:setString(title);
    TextStyleM.setArtWordTitleStyle(titleLabel);

    -- 图标
    local iconPath = getHeroIconPath(120);
    iconImg:loadTexture(iconPath);
    iconImg:setScale(1.3);

    -- 重绘按钮
    self:redrawBtn();

    -- 内容
    self:redrawDialog();
end

-- 重绘内容
function UIBSPolice:redrawDialog()
    local descLabel  = findChildByName(self.node, "CT/desc");
    local dialog = FormulaM.invoke("CALC_BS_POLICE_TALK", self.gridData.class, self.index);

    local kerning, kerning2 = 0, 0;
    if getLang() ~= "zh" and getLang() ~= "tw" then
        kerning, kerning2 = -3, -1;
    end

    -- 内容
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN, false, kerning2);
    descLabel:setString(dialog);
end

-- 重绘按钮
function UIBSPolice:redrawBtn()
    -- 建筑配置
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    local btnOk      = findChildByName(self.node, "CT/btn_ok");
    local btnCancel  = findChildByName(self.node, "CT/btn_cancel");
    local btnContinue = findChildByName(self.node, "CT/btn_one");

    if PetM.isActivePet(121) then
        -- 血狼出战
        btnOk:setVisible(false);
        btnCancel:setVisible(false);
        btnContinue:setVisible(true);

        btnContinue:setTitleText(buildingInfo["view_text"]);
    elseif self.index == 4 then
        -- 最后一组对话
        btnOk:setVisible(true);
        btnCancel:setVisible(true);
        btnContinue:setVisible(false);

        btnOk:setTitleText(getLocStr("btn_text_promise"));
        btnCancel:setTitleText(getLocStr("btn_text_refuse"));
    else
        -- 还有对话
        btnOk:setVisible(false);
        btnCancel:setVisible(false);
        btnContinue:setVisible(true);

        btnContinue:setTitleText(getLocStr("btn_text_continue"));
    end

    TextStyleM.setTextStyle(btnContinue, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    TextStyleM.setTextStyle(btnOk, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    TextStyleM.setTextStyle(btnCancel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
end










